﻿using LocalNas.Core.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using WkyApiSharp.Events.Account;
using WkyApiSharp.Service;

namespace LocalNas.Application;
public static class GlobalData
{
    public static WkyApi wkyApi;
    public static bool WkyInitComplete = false;


    public static async Task ReceiveMessage(SendMessageData data)
    {
        var hubContext = App.GetService<IHubContext<LogHub, ILogClient>>();
        await hubContext.Clients.All.ReceiveMessage(data);
        Console.WriteLine($"{data.Time:yyyyy/MM/dd HH:mm:ss.fff} =>【{data.Title}】{data.Message}");
    }

    /// <summary>
    /// 初始化玩客云
    /// </summary>
    /// <returns></returns>
    public static async Task InitWky()
    {
        //Services
        var _sysSettingRep = App.GetService<SqlSugarRepository<SysSetting>>();
        var list = await _sysSettingRep.AsQueryable().Where(x => x.Key == ConstSettingKey.玩客云账号 || x.Key == ConstSettingKey.玩客云密码).ToListAsync();
        if (list == null || list.Count != 2)
        {
            throw Oops.Oh("数据库中未找到相关配置，请更新数据库");
        }
        var user = list.FirstOrDefault(x => x.Key == ConstSettingKey.玩客云账号)?.Value;
        var password = list.FirstOrDefault(x => x.Key == ConstSettingKey.玩客云密码)?.Value;

        if (string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(password)) throw Oops.Oh("请先在设置中配置玩客云账号密码");
        wkyApi = new WkyApi(user, password, WkyLoginDeviceType.PC);
        var result = await wkyApi.StartLogin();
        if (!result) throw Oops.Oh("玩客云登录失败");

        wkyApi.EventReceived.OfType<LoginResultEvent>().Subscribe(async r =>
        {
            await ReceiveMessage(new SendMessageData
            {
                Title = "玩客云",
                Message = "登录成功",
                MessageType = MessageTypeEnum.日志,
                Data = r
            });
        });

        wkyApi.EventReceived.OfType<UpdateDeviceResultEvent>().Subscribe(async r =>
        {
            WkyInitComplete = true;
            await ReceiveMessage(new SendMessageData
            {
                Title = "玩客云",
                Message = "设备更新完毕",
                MessageType = MessageTypeEnum.日志,
                Data = r
            });
        });

        wkyApi.EventReceived.OfType<DownloadSuccessEvent>().Subscribe(async r =>
        {
            await ReceiveMessage(new SendMessageData
            {
                Title = "玩客云",
                Message = "下载完成",
                MessageType = MessageTypeEnum.玩客云任务,
                EventKey = "玩客云下载完成",
                Data = r
            });
        });

        wkyApi.EventReceived.OfType<UpdateTaskListEvent>().Subscribe(async r =>
        {
            await ReceiveMessage(new SendMessageData
            {
                Title = "玩客云",
                Message = "任务列表更新",
                MessageType = MessageTypeEnum.玩客云任务,
                EventKey = "玩客云任务",
                Data = r
            });
        });

    }


}
